﻿using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using System;
using XGame;

namespace XGame
{
    public static class HitTest
    {



        public static bool hitTestObjectRectangles(Sprite firstTarget, Sprite secondTarget)
        {
            Rectangle firstRect = new Rectangle((int)(firstTarget.Position.X - firstTarget.Width/2),
                                                (int)(firstTarget.Position.Y - firstTarget.Height/2),
                                                (int)firstTarget.Width,
                                                (int)firstTarget.Height);
           // firstRect.rotation = firstTarget.rotation;

            Rectangle secondRect = new Rectangle((int)(secondTarget.Position.X -  secondTarget.Width/2),
                                                 (int)(secondTarget.Position.Y - secondTarget.Height/2),
                                                 (int)secondTarget.Width,
                                                 (int)secondTarget.Height);

            
            //secondRect.rotation = secondTarget.rotation;

            //we should use the transformed rectagles here to get a more accurate representation of the obejcts position.

            if (firstRect.Intersects(secondRect)) return true;
            else return false;

        }


 


        public static bool HitTestPixelPerfect(Rectangle rectangleA, Color[] dataA,
                                               Rectangle rectangleB, Color[] dataB, Matrix TransformMatrix)
        {
           

            //rectangleA = CalculateBoundingRectangle(rectangleA, TransformMatrix);
           // rectangleB = CalculateBoundingRectangle(rectangleB, TransformMatrix);
            
            if (rectangleA.Intersects(rectangleB))
            {
                
            

                // Find the bounds of the rectangle intersection
                int top = Math.Max(rectangleA.Top, rectangleB.Top);
                int bottom = Math.Min(rectangleA.Bottom, rectangleB.Bottom);
                int left = Math.Max(rectangleA.Left, rectangleB.Left);
                int right = Math.Min(rectangleA.Right, rectangleB.Right);

                // Check every point within the intersection bounds
                for (int y = top; y < bottom; y++)
                {
                    for (int x = left; x < right; x++)
                    {
                        try
                        {
                            // Get the color of both pixels at this point
                            Color colorA = dataA[(x - rectangleA.Left) +
                                                 (y - rectangleA.Top) * rectangleA.Width];
                            Color colorB = dataB[(x - rectangleB.Left) +
                                                 (y - rectangleB.Top) * rectangleB.Width];

                            // If both pixels are not completely transparent,
                            if (colorA.A != 0 && colorB.A != 0)
                            {
                                return true;
                            }
                        }
                        catch (IndexOutOfRangeException)
                        {

                            return false;
                        }
                    }
                }
            }       
            else
            {
                // No intersection found
                return false;
            }

            return false;
            
            
        }


            }
}
